Sunday, February 27, 2011

UDK Programming Tutorial - Basic Gametype and Level



Setting up the Map


Requirements:

UDK 2-2011 is installed and set up correctly.

A basic UDK level, or a copy of an existing level to edit.

Some Unreal Script code.


Stage 4: Compilation!


NOTE: The Unreal Game and Unreal Editor must be closed before compiling, or you will get errors about being unable to save Game.u


Open Unreal Frontend (this is buried under Start-> All Programs-> Unreal Development Kit-> UDK-2011-02-> Tools-> Unreal Frontend).

Press the Script Button, and select Compile Scripts from the drop-down (if this doesn't work try a Full recompile, if it still doesn't work, then you need to edit your config files).



A successful compilation looks kind-of like this (above).

If you get red text then that's an error and you've done something wrong, (check your semi-colons) the error is located on the line number supplied in the error.

If you get yellow text then the compiler's giving you a warning and you're probably better off fixing it (again, the line number is supplied by frontend).


Now it's time to open the editor and take a look at what our code has done!

The editor is located in Start-> All Programs-> Unreal Development Kit-> UDK-2011-02-> UDK Editor.

Open/make a basic level (more info on this later) or download this demo level and save it to

C:\UDK\UDK-2011-02\UDKGame\Content\Maps


Now we need to edit the PIE (Play In Editor) game type, so we'll go to view->world properties.

Expand the World Info tab, and locate Game Types for PIE, select the drop-down and select Gametype (the name of your game's GameType file).



Close the World Properties and then right-click on the map (make sure you are within the level-box, or you will die), and select Play From Here.


Voilà! One working game!

At a later point I will post how to get the game to run through the UDK game, and not just the engine, but I have to re-figure that part out :S.


Next Tutorial: Making a slightly better camera.

UDK Programming Tutorial - Programming a Gametype


Setting up the Code


Requirements:

UDK 2-2011 is installed

WOTgreal is installed and has been set up correctly.

Know a little about 3D maths (enough to keep up).


Stage 3: Finally Programming something!


Note: It is VERY bad practice to overwrite existing UDK script files. The previous setup has been made so that the UDK will compile your files. To use and modify existing UDK files you extend them.

Note # 2: Some of the following code for the tutorial has been split up to multiple lines due to word wrap. Take into account that some lines that appear to be two lines, should actually be one line.

Create four text files in the folder C:\UDK\UDK-2011-02\Development\Src\Game\Classes that you made last tutorial. Name these text files

Gamesearch.uc

Gamesettings.uc

Gametype.uc

GametypePawn.uc

You must have the option “Hide extensions for known file types” unchecked to be able to do this step properly. This can be found in the following locations:

Windows 7 - Start->Control Panel->Appearance and Personalisation-> Folder Options-> View

Windows XP - Start->Control Panel-> Classic View-> Folder Options->View


Now open WOTgreal, you'll notice it is completely empty. You'll need to press UDK-> Create package/class tree.


The package and class tree should then appear on the left hand side of the screen. Ex

pand the Game section, and open the four files that we just made. In each file, write the code that follows.


Gamesearch.uc


class Gamesearch extends UTGameSearchDM;


Gamesettings.uc


class Gamesettings extends UTgameSettingsDM;


And then, because we want to actually show something, we'll add more than just extends to these files.


Gametype.uc


class Gametype extends UTDeathmatch;


defaultproperties

{

DefaultPawnClass=class'GametypePawn'

}


defaultproperties is a section of code that you will find located in most files. It is the place where you initialise global variables. Global variables are defined at the start of your code, just under your class declaration and before any function declarations (more on that later).

DefaultPawnClass is a Pawn variable declared in the GameInfo.uc class (C:\UDK\UDK-2011-02\Development\Src\Engine\Classes\GameInfo.uc)


Now we need to fill in the file called GametypePawn (I had to name it like this because Pawn and GamePawn are both existing files in the UDK, and they could get mixed up with ugly results), for the Gametype to reference. We're going to create a basic 3rd person camera.


GametypePawn.uc


class GametypePawn extends UTPawn;


simulated function bool CalcCamera(float fDeltaTime, out vector out_CamLoc, out rotator out_CamRot, out float out_FOV)

{

local vector cam_loc;

Mesh.SetOwnerNoSee(false);

cam_loc = Location - Vect(1, 0, 0) * 196.f;

out_camLoc = cam_loc;

out_CamRot = Rotator(Location - out_CamLoc);

return true;

}


simulated function bool IsFirstPerson()

{

return false;

}


DefaultProperties

{


}


CalcCamera is an inherited function in which we can override the camera's position and angle from the player. This is a very glitchy way of making a 3rd person camera, but it's also very easy, and doesn't require rewriting the Camera class.

The function accepts fDeltaTime as a parameter, and outputs out_CamLoc, out_CamRot and out_FOV as return values. Returning true will tell the camera that it has been overridden, and to use the new values supplied from the player class.


local vector cam_loc;” declares a local parameter. Any function-specific parameters that you wish to declare in Unreal Script must be declared at the beginning of the function, and must be keyworded “local”. I can't stress this enough, I have had soo many silly hard-to-figure-out errors because of this. The variable will hold the modified camera location (thus the name cam_loc) before it is assigned to out_camLoc to be returned.


Mesh.SetOwnerNoSee(false)” is VITAL if you actually want to see your character and not a disembodied gun ;). I put it here rather than in default properties because it is reset every time a character faints. It can (however) be set in default properties by using the following lines of code (which I will elaborate on later):


Begin Object Name=WPawnSkeletalMeshComponent

bOwnerNoSee=false

End Object


cam_loc = Location - Vect(1, 0, 0) * 196.f;” sets the camera to be 196 unreal units away from the player. The camera's location is (169, 0, 0) units away from the player, and is always at that angle.


out_camLoc = cam_loc;” assigns the value to the return variable (normally we'd tweak the value a bit more before this point).

out_CamRot = Rotator(Location - out_CamLoc);” Make the camera point at the player (kinda handy).


Now the code is finished! Compiling it and playing it in a level is in the next step.


Next Tutorial: Compilation!

Saturday, February 26, 2011

UDK Programming Tutorial - Setting up the UDK

Setting up the Environment


Requirements:

UDK 2-2011 is installed and has been run at least once.


Stage 2: Configuring the UDK


NOTE: Some of the following code for the tutorial has been split up to multiple lines due to word wrap. Take into account that some lines that appear to be two lines, should actually be one line.


The UDK comes with a lot of pre-written code, and this code is the basis for any UDK game. To program in the UDK you will be extending from this code, and altering it to suit your needs.


Create a new folder in C:\UDK\UDK-2011-02\Development\Src named after the game you're making, this will hold your game's code, this folder will from now-on be referred to as “Game”, you will have to make the necessary alterations to given instructions later to customise it for your own game.


Create a new folder inside that called "Classes". Create a text file, rename it to Gametype.uc.

Go to the folder C:\UDK\UDK-2011-02\UDKGame\Config and make sure that it is NOT set to "read only" (right click the file, select properties, in the General Tab then untick the Read-only checkbox. Select apply changes to subfolders and files.


All of the files in this folder should be edited with Notepad ONLY! Word and other programs add yucky stuff which will make the UDK choke when trying to recompile the files.


Open DefaultGame.ini, we're going to add some lines.


First we're going to add the gametype we've just made to the file. Do this by Adding the following line just under the other “+DefaultMapPrefixes” lines. The line is one line do not type it in on multiple lines.

+DefaultMapPrefixes=(Prefix="GPX",bUsesCommonPackage=FALSE,GameType="Game.Gametype")

This allows the UDK to “see” our code from inside a map.

NOTE: The UDK works with a system known as “Game Prefixes”, this is where you must add letters to the start of your map filename so that the UDK knows what gametype is associated with it. In this case, the prefix we're going to use is “GPX” (Game PrefiX), and all maps made for the tutorial will be labelled like so: GPX-Mapname.udk.

Add this line below the other GameSpecificMapCycles lines. The line is one line do not type it in on multiple lines.

GameSpecificMapCycles=(GameClassName="Game.Gametype",Maps=("GPX-Box"))

This is the map cycle for our game type. This adds the map GPX-Box (Which you can change to the name of any map you've made) to the gamecycle Game. So if we play our game type with a rotation of maps, it will choose this map. Multiple maps can be added, separated by a comma (no spaces) and each name enclosed in it's own quotation marks.


Now we're going to add our gametype! We will put this at the bottom of the file for easy reference, as you'll probably edit it a few times in the future. Note that any line preceded with a semi-colon (;) is a comment, and will not be read by the engine.

;///////////////

; Game Game Mode

;///////////////

[Game.Gametype UTUIDataProvider_GameModeInfo]

GameMode=Game.Gametype

GameSettingsClass=Game.Gamesettings

GameSearchClass=Game.Gamesearch

PreviewImageMarkup=UI_FrontEnd_Art.GameTypes.___TeamDeathmatch

DefaultMap=GPX-Box

Prefixes=GPX

OptionSet=DM

IconImage=UI_HUD.HUD.UI_HUD_BaseD

IconU=230

IconV=76

IconUL=104

IconVL=113


Now to break this down a little bit. The first line “[Game.Gametype UTUIDataProvider_GameModeInfo]” declares that we want to add a new game type to the UDK.

The lines

“GameMode=Game.Gametype

GameSettingsClass=Game.Gamesettings

GameSearchClass=Game.Gamesearch”

Are files that we will create. They contain the basic information for the game, as well as the pointers to all the other files needed for the game to run.

“PreviewImageMarkup=UI_FrontEnd_Art.GameTypes.___TeamDeathmatch”

Is the image that will pop up when you select the gamemode to play. You can change this image at a later point in time, but it must point to an image that's in an Unreal content package (UPK) this can be done through the Unreal Editor.

“DefaultMap=GPX-Box

Prefixes=GPX”

Sets the default map for the gametype and the prefixes that will be on all maps for this gametype. The prefix and map name can be changed depending on the name and prefix you wish to use for your game.

I'm not a hundred percent sure what the rest of the lines do, except that weird stuff happens (+ errors) if you don't include them. So I just left them at the defaults for a Death Match map.


Next up, the map!

;//////////////

; Box Map

;//////////////

[GPX-Box UTUIDataProvider_MapInfo]

MapName=GPX-Box

PreviewImageMarkup=UI_FrontEnd_Art.MapPics.___Map-Pic-vCTF-Necropolis

Description=<Strings:UTGAMEUI.CampaignBriefing.BriefDesc38>


“[GPX-Box UTUIDataProvider_MapInfo]” Lets the UDK know we want to declare a map.

“MapName=GPX-Box” tells the UDK where to find the map, the map must be in C:\UDK\UDK-2011-02\UDKGame\Content\Maps for this to work.

“PreviewImageMarkup=UI_FrontEnd_Art.MapPics.___Map-Pic-vCTF-Necropolis” is the picture to be shown when the map is selected. At current it shows the picture for Necropolis. This can be changed to another picture, so long as the picture is located in an Unreal content package (UPK), this can be done through the Unreal Editor.

“Description=<Strings:UTGAMEUI.CampaignBriefing.BriefDesc38>” This is the description for the map. It points to another .ini file. I may touch on how to edit this at a later point.


And finally, but certainly not least, is in the DefaultEngine.ini, you must add the line

+ModEditPackages=Game

below the line

+EditPackages=UTGameContent

This will point the UDK to the code so that it can compile it.

Save changes and close both files.


Lastly, you must delete all of the .ini files that start with “UDK”. Don't worry, these files are created from the base files (the ones that aren't preceded by UDK) when the Editor or Game start up, so they're not completely lost. Deleting the files will force the UDK to re-create the child-files, otherwise you probably won't see your changes take place.


Next Tutorial: Finally Programming something!